/**.
 *
 */
package com.ww.companyCabinetSystem.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ww.companyCabinetSystem.business.IAdminBusiness;
import com.ww.companyCabinetSystem.common.StatusCode;
import com.ww.companyCabinetSystem.constant.BaseConstant;
import com.ww.companyCabinetSystem.constant.ThreadPool;
import com.ww.companyCabinetSystem.model.bo.MAdminBo;
import com.ww.companyCabinetSystem.model.vo.LogMessageVo;
import com.ww.companyCabinetSystem.model.vo.ResultVo;
import com.ww.companyCabinetSystem.thread.WriteLogRunnable;
import com.ww.companyCabinetSystem.utils.JsonUtil;
import com.ww.companyCabinetSystem.utils.StringUtil;

/**
 * . com.ww.companyCabinetSystem.controller TODO
 * 
 * @author veloster 上午11:57:04
 */
@Controller
@RequestMapping(value = "/AdminController")
public class AdminController extends BaseController {

	/**
	 * . adminBusiness
	 */
	@Resource(name = "adminBusiness")
	private IAdminBusiness adminBusiness;

	/**
	 * 注册.
	 * 
	 * @param reqeust
	 * @param adminVo
	 * @return
	 */
	@RequestMapping(value = "/regist", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
	@ResponseBody
	public String regist(HttpServletRequest request, String adminVo) {
		try {
			if (StringUtil.strIsNullOrEmpty(adminVo)) {
				return toFailure(StatusCode.PARAMETER_ERROR, "入参为空");
			}
			MAdminBo bo = JsonUtil.toObject(adminVo, MAdminBo.class);
			ResultVo resultVo = adminBusiness.regist(bo);
			if (resultVo == null) {
				return toFailure(StatusCode.PARAMETER_ERROR, "入参为空");
			}
			if (!resultVo.getFlag()) {
				if (resultVo.getStatusCode() == null) {
					return toFailure(StatusCode.BAD_REQUEST, "未知错误");
				}
				switch (resultVo.getStatusCode()) {
				case BaseConstant.REGISTINFO_USEREXITS:
					return toFailure(BaseConstant.REGISTINFO_USEREXITS, "用户名已存在");
				case BaseConstant.REGISTINFO_INSERTERROR:
					return toFailure(BaseConstant.REGISTINFO_INSERTERROR, "内部异常");
				default:
					return toFailure(StatusCode.BAD_REQUEST, "未知错误");
				}
			}
			return toSuccess(true, "操作成功");
		} catch (Exception e) {
			// 异步写日志
			LogMessageVo vo = new LogMessageVo();
			vo.setMessage(e.getMessage());
			vo.setStatusCode(BaseConstant.LOGSTATUS_ERROR);
			ThreadPool.getInstance().loadTasks(new WriteLogRunnable(vo));
			return toFailure("内部异常");
		}
	}

	/**
	 * 登陆.
	 * 
	 * @param request
	 *            请求
	 * @param adminVo
	 *            入参数据
	 * @return str
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
	@ResponseBody
	public String login(HttpServletRequest request, String adminVo) {
		try {
			if (StringUtil.strIsNullOrEmpty(adminVo)) {
				return toFailure(StatusCode.PARAMETER_ERROR, "入参为空");
			}
			MAdminBo bo = JsonUtil.toObject(adminVo, MAdminBo.class);
			ResultVo<MAdminBo> result = adminBusiness.login(bo);
			if (result == null) {
				return toFailure(StatusCode.PARAMETER_ERROR, "入参为空");
			}
			if (result.getFlag()) {
				request.getSession().setAttribute("LoginStaff", result.getContent());
			}
			return toSuccess(result.getFlag());
		} catch (Exception e) {
			// 异步写日志
			LogMessageVo vo = new LogMessageVo();
			vo.setMessage(e.getMessage());
			vo.setStatusCode(BaseConstant.LOGSTATUS_ERROR);
			ThreadPool.getInstance().loadTasks(new WriteLogRunnable(vo));
			return toFailure("内部异常");
		}
	}
}
